package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll() {
        List<Role> roles= roleDao.findAll();
        return roles;
    }

    //添加检查组合，同时需要设置检查组合和检查项的关联关系
    public void add(Role role, Integer[] persmissionIds, Integer[] menuIds) {
        roleDao.add(role);
        setPermissionAndRole(role.getId(),persmissionIds);
        setMenuAndRole(role.getId(),menuIds);
    }

    //设置角色和权限项的关联关系
    public void setPermissionAndRole(Integer roleId, Integer[] persmissionIds){
        if(persmissionIds != null && persmissionIds.length > 0){
            for (Integer persmissionId : persmissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("permission_id", persmissionId);
                roleDao.setPermissionAndRole(map);
            }
        }
    }

    //设置角色和菜单项的关联关系
    public void setMenuAndRole(Integer roleId, Integer[] menuIds){
        if(menuIds != null && menuIds.length > 0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("menu_id", menuId);
                roleDao.setMenuAndRole(map);
            }
        }
    }

    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

    //编辑检查组，同时需要更新和检查项的关联关系
    public void edit(Role role, Integer[] persmissionIds, Integer[] menuIds) {
        //根据检查组id删除中间表数据（清理原有关联关系）
        roleDao.deletePermissionByRole(role.getId());
        roleDao.deleteMenuByRole(role.getId());
        //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
        setPermissionAndRole(role.getId(), persmissionIds);
        setMenuAndRole(role.getId(), menuIds);
        //更新检查组基本信息
        roleDao.edit(role);
    }

    //删除
    public void delete(Integer id) throws RuntimeException{
        //查询当前检查项是否和检查组关联
        long count = roleDao.findCountByRoleId(id);
        if(count > 0){
            //当前检查项被引用，不能删除
            throw new RuntimeException("当前角色下有用户，不能删除");
        }
        roleDao.deletePermissionByRole(id);
        roleDao.deleteMenuByRole(id);
        roleDao.deleteById(id);
    }
}
